
import {Tree, Loading} from 'element-ui'

export default {
  components: {Tree, Loading},
  props: {
    pageParams: {
      type: Object,
      default: () => {
        return {}
      },
    },
    pageHeight: Number,
    pageWidth: Number,
  },
  data() {
    return {
      leftWidth: 250,
      dataRowsQueryUrl: '',
      dataRowsDeleteUrl: '',
      dataRowsUpdateUrl: '',
      dataRowsSortUrl: '',
      dataRowsLoading: false,
      dataRows: null,
      currentNodeKey: null,
      expandedKeys: [],
      showCheckbox: false,
      queryDataRows: window.$debounce(this._queryDataRows),
      queryParams: {
        keywords: ''
      },
      dataTreeClickCount: 0,
      doDataTreeDblClick: window.$debounce(this._doDataTreeDblClick, 250),
      formView: null,
      formParams: {},
    }
  },
  computed: {
    rightWidth() {
      return this.pageWidth - this.leftWidth
    }
  },
  watch: {
    queryParams: {
      handler: function () {
        this.queryDataRows()
      },
      deep: true
    },
  },
  created() {
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.queryDataRows()
    },
    doSearch() {
      this.queryDataRows()
    },
    _queryDataRows() {
      if (!this.dataRowsQueryUrl) {
        return
      }
      this.dataRowsLoading = true
      this.$http.post(this.dataRowsQueryUrl, this.queryParams).then((rs) => {
        this.dataRowsLoading = false
        const result = rs.data
        if (result.success) {
          this.dataRows = result.data
          this.doQuerySuccess()
        } else {
          this.dataRows = []
        }
      })
    },
    doQuerySuccess() {
      if (this.$refs.tree) {
        this.$nextTick(() => {
          this.$refs.tree.setCurrentKey(this.currentNodeKey)
        })
      }
    },
    _doSelect(data) {
      if (data) {
        this.currentNodeKey = data.key
        this.formParams = {id: data.id}
      } else {
        this.currentNodeKey = null
        this.formParams = {}
      }
    },
    _doDataTreeDblClick(data) {
      // 处理单击
      this._doSelect(data)
      // 处理双击
      if (this.dataTreeClickCount >= 2) {
        const node = this.$refs.tree.store.nodesMap[data.id]
        node.expanded = !node.expanded
      }
      this.dataTreeClickCount = 0;
    },
    doSelect(data) {
      this.dataTreeClickCount++;
      this.doDataTreeDblClick(data)
    },
    doExpand(data) {
      this.expandedKeys.push(data.id)
    },
    doCollapse(data) {
      this.expandedKeys.forEach((item, index, arr) => {
        if(item == data.id) {
          arr.splice(index, 1);
        }
      });
    },
    doUnselect() {
      this.doSelect(null)
      this.$refs.tree.setCurrentKey(null)
    },
    doDrop(dragNode, dropNode, position) {
      this.move(dragNode.data, dropNode.data, position)
    },
    move(dragData, dropData, position) {
      let url
      let formData
      switch(position) {
      case 'before' :
        url = this.dataRowsSortUrl
        formData = {
          prevRecordId: '',
          currRecordId: dragData.id,
          nextRecordId: dropData.id
        }
        break
      case 'after' :
        url = this.dataRowsSortUrl
        formData = {
          prevRecordId: dropData.id,
          currRecordId: dragData.id,
          nextRecordId: ''
        }
        break
      case 'inner':
        url = this.dataRowsUpdateUrl + dragData.id
        formData = {
          id: dragData.id,
          parentId: dropData.id,
        }
        break
      }

      this._submitMove(url, formData)
    },
    _submitMove(url, formData) {
      if (!url) {
        return false
      }
      this.$http.post(url, formData).then((rs) => {
        const result = rs.data
        if (result.success) {
          this.$message.success(result.message)
          // this.init()
        } else {
          this.$message.error(result.message)
          this.init()
        }
      })
    },
    // 子类可以重写该函数
    doAdd(parentData) {
      this._doAdd(parentData)
    },
    _doAdd(parentData) {
      if (parentData) {
        this.currentNodeKey = parentData.key
        this.formParams = {
          parentId: parentData.id,
          parentName: parentData.fullName || parentData.name
        }
      } else {
        this.currentNodeKey = ''
        this.formParams = {}
      }
      this.$refs.tree.setCurrentKey(this.currentNodeKey)
    },
    getCheckedKeys() {
      return this.$refs.tree.getCheckedKeys()
    },
    doDelete(record) {
      let url = this.dataRowsDeleteUrl
      let checkedKeys = null
      if (record && record.id) {
        url += record.id
        this._delete(url)
      } else {
        url += '-1'
        checkedKeys = this.getCheckedKeys()
        if (checkedKeys.length > 0) {
          this.$alert.confirm({
            title: '您确定要删除选中的数据吗？',
            onOk: () => {
              this._delete(url, checkedKeys)
            }
          })
        }
      }
    },
    _delete(url, checkedKeys) {
      this.$http.post(url, checkedKeys).then((rs) => {
        const result = rs.data
        if (result.success) {
          this.$message.success(result.message)
          this.queryDataRows()
        } else {
          this.$message.error(result.message)
        }
      })
    },
    doUncheck() {
      this.showCheckbox = false
      this.$refs.tree.setCheckedKeys([])
    },
    doFormSuccess(data) {
      this.queryDataRows()
      if (!data) {
        this.doSelect(null)
      } else {
        this.expandedKeys.push(data.parentId)
      }
    },
    doFormClose(data) {
      if (!data) {
        this.doAdd()
      }
    },
  }
}